<template>
  <!-- 新建物料单 -->
  <HeaderNavTabarVue   :title="title" />
  <view
    :style="'min-height: ' + boxHeight + 'rpx;'"
    class="designDrawings"
    @touchmove.stop.prevent="() => {}"
  >
    <view class="uploadImage">
      <view class="materialImage-title">
        <view class="isrequired">上传设计稿</view>
        <text>最多上传6张</text>
      </view>
      <view class="materialImage-upload">
        <UploadFile @getSuccessUrl="getSuccessUrl" @onDelete="onDelete" />
      </view>
    </view>
    <view class="subData">
      <button @tap="subMaterial">直接提交</button>
    </view>
  </view>
</template>

<script setup>
//
import { ref } from 'vue'
import { addMeasurement } from '@/api/customer.js'
import { onLoad } from '@dcloudio/uni-app'
import HeaderNavTabarVue from '@/components/HeaderNavTabar/HeaderNavTabar.vue'
import UploadFile from '@/components/UploadFile/UploadFile.vue'
import { getTitleBarHeight } from '@/utils/system.js'
import config from '@/config.js'
const title = '新建物料单 - 上传设计稿'

// 页面内容高度
const boxHeight =
  (uni.getSystemInfoSync().windowHeight - getTitleBarHeight()) * 2
let materialParamsAll = ref(null)

let designDrawings = ref([])
const systemInfo = uni.getSystemInfoSync()
const isAndroid = systemInfo.platform.toLowerCase() === 'android'

const addMeasurementFn = async (params) => {
  params.mtype = '物料类'
  params.material = isAndroid ? '安卓':"苹果"
  let { code, msg } = await addMeasurement(params)
  if (code == 200) {
    uni.showToast({
      title: msg,
      icon: 'success',
    })
    setTimeout(() => {
      uni.switchTab({
        url: '/pages/work/index',
      })
    }, 500)
  } else {
    uni.showToast({
      title: '操作失败',
      icon: 'error',
    })
  }
}
// 获取上传的图片
const getSuccessUrl = (fileName) => {
  designDrawings.value.push(config.baseUrl + fileName)
}
// 删除图片后的数据
const onDelete = (emit) => {
  designDrawings.value = emit
}
// 提交
const subMaterial = () => {
		if(designDrawings.value.length > 0){
			materialParamsAll.value.materialImage = JSON.stringify(designDrawings.value)
			const obj = JSON.parse(materialParamsAll.value.materialList)
			const {
			  actualTotalInstall,
			  actualTotalMaterial,
			  actualUrbanManagementFee,
			  moneyAll,
			  moneyCity,
			  moneyInstall,
			  moneyMaterials,
			  businessObj,
			} = obj
			materialParamsAll.value.actualTotalInstall = actualTotalInstall
			materialParamsAll.value.actualTotalMaterial = actualTotalMaterial
			materialParamsAll.value.actualUrbanManagementFee = actualUrbanManagementFee
			materialParamsAll.value.moneyAll = moneyAll
			materialParamsAll.value.moneyCity = moneyCity
			materialParamsAll.value.moneyInstall = moneyInstall
			materialParamsAll.value.moneyMaterials = moneyMaterials
			materialParamsAll.value.materialList = JSON.stringify(businessObj)
			// console.log('materialParamsAll.value', materialParamsAll.value)
			addMeasurementFn(materialParamsAll.value)
		}else{
			uni.showToast({
				title:'请上传设计稿',
				icon:'error'
			})
		}
  
}

onLoad((option) => {
  if (option.oneViewParams) {
    materialParamsAll.value = JSON.parse(
      decodeURIComponent(option.oneViewParams),
    )
  }
})
</script>

<style lang="scss" scoped>
@import '/static/style/index.scss';
.designDrawings {
  padding: $spacing-row-base;
  box-sizing: border-box;
  // @include main-area-bgColor;
  overflow-y: auto;
  background-color: $uni-bai;

  .uploadImage {
    // @include background-color;
    background-color: $uni-bg-lin;
    padding: $spacing-row-base $spacing-col-base;
    border-radius: 16rpx;

    .materialImage-title {
      @include flex(center, space-between);
      margin-bottom: $spacing-row-base;

      h4 {
        font-weight: 400;
        // @include font-color;
        color: $uni-fc-lin;
      }

      text {
        // @include font-color-auxiliary;
        color: $uni-fc-fu-lin;
      }
    }

    .materialImage-upload {
    }
  }

  .subData {
    width: 100%;
    margin-top: 40rpx;

    button {
      width: 100%;
      @include btnBg;
    }
  }
}
</style>
